package com.mdoya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdoya.domain.DishFlavor;
import com.mdoya.dto.DishDto;
import com.mdoya.mapper.DishFlavorMapper;
import com.mdoya.service.DishFlavorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@Scope("singleton")
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class DishFlavorServiceImpl implements DishFlavorService {
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Integer add(DishFlavor dishFlavor) {
        return dishFlavorMapper.insert(dishFlavor);
    }

    /**
     * 根据菜品表id查询 口味记录
     *
     * @param dishId
     * @return
     */
    @Override
    public List<DishFlavor> findByDishIdDishFlavo(Long dishId) {
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
        return dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
    }

    /**
     * 根据dish表中的更新口味
     *
     * @param dishId
     * @return
     */
    @Override
    public Integer updateDishFlavor(Long dishId) {
        log.info("Dish更新口味: {}",dishId);
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
        return dishFlavorMapper.delete(dishFlavorLambdaQueryWrapper);
    }

    /**
     * 更新 删除所有口味
     *
     * @param dishId
     * @return
     */
    @Override
    public Integer deleteDishFlavor(Long dishId) {
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
        return dishFlavorMapper.delete(dishFlavorLambdaQueryWrapper);
    }
}
